package br.ucb.sigplan.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.ucb.sigplan.beans.Curso;
import br.ucb.sigplan.connection.FactoryCon;
import br.ucb.sigplan.dao.CursoDAO;
import br.ucb.sigplan.exception.DAOException;

public class CursoDAOImpl implements CursoDAO {
	private final String SQL_INSERT = "insert into curso (nome, codigo) values (?, ?);";
	private final String SQL_FINDALL = "select * from curso";
	private final String SQL_SEARCHBYID = "select * from curso where idcurso = ?";
	private final String SQL_SEARCHBYNOME = "select * from curso where nome = ? ";
	private final String SQL_UPDATE = "update curso set nome = ?, codigo = ? where idcurso = ?";
	private final String SQL_DELETE = "delete from curso where idcurso = ?";
	
	public List<Curso> findAll() throws DAOException {
		List<Curso> cursos =  new ArrayList<Curso>();
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		Curso curso;
		ResultSet set = null;
		try {
			instrucao = con.prepareStatement(SQL_FINDALL);
			set = instrucao.executeQuery();
			while (set.next()) {
				curso = new Curso();
				putCurso(set, curso);
				cursos.add(curso);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao,set);
		}
		return cursos;
	}

	public void insert(Curso curso) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_INSERT);
			instrucao.setString(1, curso.getNome());
			instrucao.setInt(2, curso.getCodigo());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroInsercao");
		}finally{
			FactoryCon.closeConnection(con, instrucao);
		}
	}
	
	public void putCurso(ResultSet set, Curso curso) throws SQLException {
		curso.setIdCurso(set.getInt("idcurso"));
		curso.setNome(set.getString("nome"));
		curso.setCodigo(set.getInt("codigo"));
	}

	public Curso findById(Integer idcurso) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		Curso curso = null;
		ResultSet set = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYID);
			instrucao.setInt(1, idcurso);
			set = instrucao.executeQuery();
			while (set.next()) {
				curso = new Curso();
				putCurso(set, curso);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao,set);
		}
		return curso;
	}

	public Curso findByNome(String nome) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		Curso curso = null;
		ResultSet set = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYNOME);
			instrucao.setString(1, nome);
			set = instrucao.executeQuery();
			while (set.next()) {
				curso = new Curso();
				putCurso(set, curso);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao,set);
		}
		return curso;
	}

	public boolean isCursoCadastrado(String nome){
		Curso curso = findByNome(nome);
		if (curso != null) {
			return true;
		}
		return false;
	}

	@Override
	public void update(Curso curso) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_UPDATE);
			instrucao.setString(1, curso.getNome());
			instrucao.setInt(2, curso.getCodigo());
			instrucao.setInt(3, curso.getIdCurso());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroAtualizacao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
	}

	@Override
	public void delete(Integer idCurso) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_DELETE);
			instrucao.setInt(1, idCurso);
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			 throw new DAOException("cursoDelecao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
		
	}

}
